<?php
/**
 * Created by PhpStorm.
 * User: Zhanghaiqiang
 * Date: 2020-03-08
 * Time: 22:47
 */

namespace App\Core\Endpoints\CandyStory;


use Endpoints\Base\Endpoint;
use Endpoints\Tools\GetOssObject;
use Models\Candy\Story;
use Models\Candy\StoryImages;
use Symfony\Component\Console\Input\InputArgument;

class IndexStoryImages extends Endpoint
{
// 定义参数
    const ARGUMENT_STORY_ID   = 'story_id';


    // 收参
    public function getArguments()
    {
        return [
            [static::ARGUMENT_STORY_ID,InputArgument::REQUIRED,'故事id',''],
        ];
    }

    /**
     * 实现逻辑
     * @return \Laravelladder\Core\Collections\BaseCollection
     */
    public function dryRun()
    {
        $storyId= $this->argument(static::ARGUMENT_STORY_ID);

        $list = $this->getCandyStoryImagesRepo()->getListByFilters(
            [["story_id","=",$storyId]],10,0,['sort' => 'asc','id' => 'asc']);

        $list->each(function($item){
            /**
             * @var $item StoryImages
             */
            if($item->{StoryImages::DB_FIELD_IMAGE_SOURCE} == StoryImages::IMAGE_SOURCE_ALIYUN){
                $imageUrl = GetOssObject::getInstance()->setArguments([
                    GetOssObject::ARGUMENT_FILE_ID => $item->{StoryImages::DB_FIELD_IMAGE_VALUE},
                    GetOssObject::ARGUMENT_IMG_OPTION => 'image/resize,m_lift,h_500,w_500',
                ])->dryRun();
                $imagesUrl = $imageUrl;
            }else{
                $imagesUrl = $item->{StoryImages::DB_FIELD_IMAGE_VALUE};
            }
            $item->addFillable('image_url',$imagesUrl);
        });
        return $list;
    }
}